package com.qingfeng.base.secuity;

import com.qingfeng.base.aop.LogInfo;
import com.qingfeng.base.dao.TMenuResourceMapper;
import com.qingfeng.base.model.TMenuResource;
import com.qingfeng.client.bean.Seller;
import com.qingfeng.client.dao.SellerMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author:zhangyuefei
 * @Date:2019/9/6 15:39
 */
@Api("我的基本信息")
@Component
public class MyUserDetailsService implements UserDetailsService {


    @Autowired
    private SellerMapper userMapper;

    @Autowired
    private TMenuResourceMapper tMenuResourceMapper;


    /**
     * 登录验证
     *
     * @param s
     * @return
     * @throws UsernameNotFoundException
     */
    @LogInfo(param = "'用户'+#s+'登录'")
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("===================================用户登录:"+s);
        Seller user = userMapper.loginUserByAccount(s);
        if (user==null){
            return null;
        }
        List<TMenuResource> list;
        if (s.trim().toUpperCase().equals("ADMIN")) {
            list = tMenuResourceMapper.selectAll();
        }else {
            list = tMenuResourceMapper.selectAllByUserId(user.getId());
        }
        user.setResources(list);

        return user;
    }
}
